﻿using System;
using System.IO;
using System.Web;
using Cms.Application.Admin.Views;
using Cms.Application.IO;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;
using iLib.Web.UI;

namespace Cms.Application.Admin.Presenters
{
    public class ProcessUploadedFilePresenter : BasePresenter
    {
        private readonly Administrator _administrator;
        private readonly IProcessUploadedFileView _view;
        public IDiskAccess DiskAccess;

        public ProcessUploadedFilePresenter(IProcessUploadedFileView view)
            : this(view, new RepositoryFactory())
        {
            DiskAccess = new DiskAccess();
        }

        public ProcessUploadedFilePresenter(IProcessUploadedFileView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
            if (!_administrator.HasPermission.Upload)
                _view.RedirectToPermissionDeniedPage();
        }

        public void SaveUploadedFile(HttpPostedFile file)
        {
            MediaFolder folder = MediaRepository.GetMediaFolder(_administrator.SelectedMediaFolderID);
            string path = folder == null ? _administrator.CurrentSite.RootMediaFolder : folder.GetPhysicalPath();
            string fileName = Path.GetFileName(file.FileName).Replace(" ", "-");
            DiskAccess.DeleteFile(path + "\\" + fileName);
            file.SaveAs(Url.PhysicalPath + path + "\\" + fileName);
            MediaFile mediaFile = folder == null
                                      ? _administrator.CurrentSite.GetRootMediaFile(fileName)
                                      : folder.GetFile(fileName);
            if (mediaFile == null)
            {
                mediaFile = new MediaFile
                                {
                                    Site = _administrator.CurrentSite,
                                    Name = fileName,
                                    Extension = fileName.Substring(fileName.LastIndexOf(".") + 1),
                                    Folder = folder
                                };
                if (folder != null)
                {
                    folder.Files.Add(mediaFile);
                }
                var fileInfo = new FileInfo(Url.PhysicalPath + path + "\\" + fileName);
                mediaFile.Size = fileInfo.Length;
                mediaFile.UploadedTimestamp = DateTime.Now;
                _administrator.CurrentSite.MediaFiles.Add(mediaFile);
            }
            else
            {
                mediaFile.Version = DateTime.Now.Ticks.ToString();
            }
            MediaRepository.SaveMediaFile(mediaFile);
            SiteRepository.SaveSite(_administrator.CurrentSite);

            // Possible improvement: check if the file existed prior to the upload
            _view.RemoveFileFromOutputCache(mediaFile.GetVirtualPath());
        }
    }
}